package acm.广度优先搜索;

import 左哥算法.ch15并查集.UnionFindSet;

import java.util.*;

/**
 * 并查集解法
 */
public class I {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()) {
            int n= sc.nextInt();
            int m= sc.nextInt();
            if (n+m==0)return;
            sc.nextLine();
            char[][] maze=new char[n][m];
            for (int i = 0; i < maze.length; i++) {
                maze[i]=sc.nextLine().toCharArray();
            }
            System.out.println(fun(maze));
            System.out.println(fun2(maze));
        }
    }
    public static byte[][] directions ={
            {-1,-1},    //左上方
            {-1,0},      //上方
            {-1,1},      //右上方
            {0,-1},     //左方
            {0,1},      //右方
            {1,-1},     //左下方
            {1,0},      //下方
            {1,1},      //右下方
    };
    public static int fun(char[][] maze){
        UnionFindSet<Node> unionSet = new UnionFindSet();
        for (byte i = 0; i < maze.length; i++) {
            for (byte j = 0; j < maze[i].length; j++) {
                if (maze[i][j]=='@'){   //当前是油袋
                    Node local = new Node(i, j);
                    unionSet.init(local);
                    for (byte[] d : directions) {   //查看附近是否存在油田
                        Node next = new Node((byte) (i + d[0]), (byte) (j + d[1]));
                        if (    next.x>=0&&next.x<maze.length         //未越界
                                &&next.y>=0&&next.y<maze[0].length    //未越界
                                &&maze[next.x][next.y]=='@'           //附近存在的油田
                            ){
                                unionSet.union(local,next);             //合并
                        }
                    }
                }
            }
        }
        return unionSet.getSetCount();
    }
    public static int fun2(char[][] maze){
        int res=0;
        for (int i = 0; i < maze.length; i++) {
            for (int j = 0; j < maze[0].length; j++) {
                if (maze[i][j]=='@'){
                    res++;
                    union(i,j,maze);
                }
            }
        }
        return res;
    }
    public static void union(int x,int y,char[][] maze){
        if (x<0||x>=maze.length||y<0||y>=maze[0].length||maze[x][y]=='*'){
            return;
        }
        maze[x][y]='*';
        for (byte[] d : directions) {   //把附近油田熄灭
            union(x+d[0],y+d[1],maze);
        }
    }
}
class Node{
    byte x;
    byte y;

    public Node(byte x, byte y) {
        this.x = x;
        this.y = y;
    }

    public Node(byte[] node) {
        this.x = node[0];
        this.y = node[1];
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Node node = (Node) o;
        return x == node.x && y == node.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }
}
class Test{
    public static void main(String[] args) {

    }
    static {
        compare();
    }
    public static void compare(){
        Random random=new Random();
        int count=10000;
        while (count-- > 0) {
            char[][] maze=new char[random.nextInt(100)+1][random.nextInt(100)+1];
            for (char[] chars : maze) {
                for (int i = 0; i < chars.length; i++) {
                    chars[i]=random.nextBoolean()?'@':'*';
                }
            }
            char[][] a1 = new char[maze.length][maze[0].length];
            for (int i = 0; i < a1.length; i++) {
                a1[i]=maze[i].clone();
            }
            char[][] a2 = new char[maze.length][maze[0].length];
            for (int i = 0; i < a1.length; i++) {
                a2[i]=maze[i].clone();
            }
            int fun2 = I.fun2(a2);
            int fun = I.fun(a1);
            if (fun!=fun2){
                System.out.println("预期："+fun2+";实际："+fun);
                for (char[] chars : maze) {
                    System.out.println(new String(chars));
                }
            }
        }
    }
}